<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

if(!defined('TinyFW')) exit('Access denied!');

class tfError{

    private static $instance = null;
    /*
    *  Predefined error constants
    *  http://usphp.com/manual/en/errorfunc.constants.php
    * *
    1     E_ERROR
    2     E_WARNING
    4     E_PARSE
    8     E_NOTICE
    16    E_CORE_ERROR
    32    E_CORE_WARNING
    64    E_COMPILE_ERROR
    128   E_COMPILE_WARNING
    256   E_USER_ERROR
    512   E_USER_WARNING
    1024  E_USER_NOTICE
    2048  E_STRICT
    4096  E_RECOVERABLE_ERROR
    8192  E_DEPRECATED
    16384 E_USER_DEPRECATED
    30719 E_ALL
    */


    public static function singleton(){
        if(!isset(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public static function tfErrorHandler($errno, $errstr, $errfile, $errline){
        return self::singleton()->_tf_error_handler($errno, $errstr, $errfile, $errline);
    }

    public static function tfExceptionHandler($e) {
        return self::singleton()->_tf_exception_handler($e);
    }

    public static function setErrorExceptionHandler() {
        // set error handler
        set_error_handler(array('tfError','tfErrorHandler'));
        // set exception handler
        set_exception_handler(array('tfError','tfExceptionHandler'));
        
    }

    // Obullo error template for error handling.
    private function _tf_error_template($errno, $errstr, $errfile, $errline, $type) {
        $msg = '<div style=\'width:50%;padding:5px;background-color:#eee;\'>';
        $msg.= '<b>[TinyFW]['. ucwords(strtolower($type)).']:</b>'.$errstr.'<br />';
        $msg.= '<b>File:</b> '.$errfile."<br />";
        $msg.= '<b>Line:</b> '.$errline;
        $msg.= '</div>';
        echo $msg;
    }

    //  error template for exceptions.
    private function _tf_exception_handler($e) {
        $type = 'Exception';
        $sql_string  = '';
        //if(substr($e->getMessage(),0,3) == 'SQL')
        //{
        //    $type = 'Database';
            //$ob = ob::instance();
        //    $sql_string = '<br /><b>SQL: </b> &nbsp;';
        //}

        $msg = '<div style=\'width:50%;padding:5px;background-color:#eee;\'>';
        $msg.= '<b>['. $type .' Error]: </b>'.$e->getMessage().'<br />';
        if($e->getCode() != 0)
        $msg.= '<b>Code:</b> '.$e->getCode()."<br />";
        $msg.= '<b>File:</b> '.$e->getFile()."<br />";
        $msg.= '<b>Line:</b> '.$e->getLine();
        $msg.= $sql_string;
        $msg.= '</div>';

        echo $msg;
    }

    /**
    * 404 Page Not Found Handler
    *
    * @access    private
    * @param    string
    * @return    string
    */
    function show_404($page = '') {
        $heading = "404 Page Not Found";
        $message = "The page '".$page."' you requested was not found.";

        $msg = '<div style=\'width:50%;padding:5px;background-color:#eee;\'>';
        $msg.= '<b>['.$heading.']: </b>'.$message.'<br />';
        $msg.= '</div>';
        echo $msg;
        exit;
    }

    /*
    *  Main error handler function.
    */
    private function _tf_error_handler($errno, $errstr, $errfile, $errline) {
        if (($errno & error_reporting()) == 0) return;

        switch ($errno) {
        case E_ERROR:
            $this->_tf_error_template($errno, $errstr, $errfile, $errline, "ERROR");
        break;

        case E_WARNING:
            $this->_tf_error_template($errno, $errstr, $errfile, $errline, "WARNING");
        break;

        case E_PARSE:
            $this->_tf_error_template($errno, $errstr, $errfile, $errline, "PARSE ERROR");
        break;

        case E_NOTICE:
            $this->_tf_error_template($errno, $errstr, $errfile, $errline, "NOTICE");
        break;

        case E_CORE_ERROR:
            $this->_tf_error_template($errno, $errstr, $errfile, $errline, "CORE ERROR");
        break;

        case E_CORE_WARNING:
            $this->_tf_error_template($errno, $errstr, $errfile, $errline, "CORE WARNING");
        break;

        case E_COMPILE_ERROR:
            $this->_tf_error_template($errno, $errstr, $errfile, $errline, "COMPILE ERROR");
        break;

        case E_USER_ERROR:
            $this->_tf_error_template($errno, $errstr, $errfile, $errline, "USER FATAL ERROR");
            exit();
        break;

        case E_USER_WARNING:
            $this->_tf_error_template($errno, $errstr, $errfile, $errline, "USER WARNING");
        break;

        case E_USER_NOTICE:
            $this->_tf_error_template($errno, $errstr, $errfile, $errline, "USER NOTICE");
        break;

        case E_STRICT:
            $this->_tf_error_template($errno, $errstr, $errfile, $errline, "STRICT ERROR");
        break;

        case E_RECOVERABLE_ERROR:
            $this->_tf_error_template($errno, $errstr, $errfile, $errline, "RECOVERABLE ERROR");
        break;

        case E_DEPRECATED:
            $this->_tf_error_template($errno, $errstr, $errfile, $errline, "DEPRECATED ERROR");
        break;

        case E_USER_DEPRECATED:
            $this->_tf_error_template($errno, $errstr, $errfile, $errline, "USER DEPRECATED ERROR");
        break;

        case E_ALL:
            $this->_tf_error_template($errno, $errstr, $errfile, $errline, "ERROR");
        break;

        }

        return TRUE;
    }

   
}


?>
